﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CDStore3Tiers;
using System.Data.Linq;

namespace CuaHangDiaNhac
{
	public partial class frmQuanLyHoaDon : DevComponents.DotNetBar.Office2007Form
	{
		log4net.ILog log = log4net.LogManager.GetLogger("Hóa đơn");
		// frmChiTietHoaDon frmChiTiet = null;
		StoreCDDataContext dt;
		List<vwDiaNhacBan> ListDia = new List<vwDiaNhacBan>();
		frmInHoaDonBanHang inhoadon;
		// List<DSGiamGia> ListGiamGia = new List<DSGiamGia>();
		//List<SuKien> ListSuKien = new List<SuKien>();
		//List<ChiTietHoaDonBanHang> ListChiTietHD = new List<ChiTietHoaDonBanHang>();

		public void lockLogin()
		{
			string id = frmLogin.id;
			XLPhanQuyen phanquyen = new XLPhanQuyen();
			List<TaiKhoanChucNang> tkcn = new List<TaiKhoanChucNang>();
			tkcn = phanquyen.TKChucNang(frmLogin.id);


			if (phanquyen.CoQuyen(id, 702))
			{
				btnXoa.Enabled = true;
				btnXoaHD.Visible = true;
				btnXoadt.Enabled = true;
			}
			if (phanquyen.CoQuyen(id, 707))
			{
				btnIn.Enabled = true;
				btnInHoaDon.Enabled = true;
			}
		}

		public void closeall()
		{
			btnXoa.Enabled = false;
			btnXoaHD.Visible = false;
			btnXoadt.Enabled = false;
			btnIn.Enabled = false;
			btnInHoaDon.Enabled = false;
		}



		public void lockLogin(bool val, int pq)
		{
			if (pq == 1)
			{
				btnXoadt.Enabled = val;
				btnThongKe.Enabled = val;
				btnXoa.Enabled = val;
			}
			else
			{
				btnXoadt.Enabled = !val;
				btnThongKe.Enabled = !val;
				btnXoa.Enabled = !val;
			}

		}
		frmMain frm;
		public frmQuanLyHoaDon()
		{
			InitializeComponent();
			dt = new StoreCDDataContext();
			dt.Connection.ConnectionString = getConfig.getConnection();
			lockLogin(true, frmLogin.qh);
			this.dgrDanhSach.Sort(this.dgrDanhSach.Columns["Mahd"], ListSortDirection.Descending);
		}
		private void QuanLyHoaDon_Load(object sender, EventArgs e)
		{
			closeall();
			lockLogin();
			frm = MdiParent as frmMain;
			BintoGrv();
			log.Info(frmLogin.id + ": đã load lên form hóa đơn");
			// this.dgrDanhSach.Sort(this.dgrDanhSach.Columns["Mahd"], ListSortDirection.Descending);
		}
		void BintoGrv()
		{
			var hoadon = XuLyHoaDon.HoaDon_LayTatCa();
			BindingSource bds = new BindingSource();
			bds.DataSource = hoadon;
			bindingNavigatorEx1.BindingSource = bds;
			dgrDanhSach.DataSource = bds;
			dgrDanhSach.DataSource = dt.HoaDonBan_SelectAll();
			this.dgrDanhSach.Sort(this.dgrDanhSach.Columns["Mahd"], ListSortDirection.Descending);
		}
		private void btnDSHoaDon_Click(object sender, EventArgs e)
		{
			BintoGrv();
		}
		DataGridViewRow r;

		void Getdata()
		{
			if (r != null)
			{
				r = dgrDanhSach.CurrentRow;

				txtNDHD.Text = ConvertType.ToString(r.Cells["ND"].Value);

				dttNgay.Value = Convert.ToDateTime(r.Cells["Ngay"].Value);
			}
			else
			{
				r = dgrDanhSach.CurrentRow;

				Reset();
			}
		}
		private void dgrDanhSach_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
		{
			//XuLyDataGridView.CheckRow(dgrDanhSach, RowSelect, e);
			r = dgrDanhSach.CurrentRow;
			Getdata();


		}
		List<int> RowSelect = new List<int>();
		private void btnXoadt_Click(object sender, EventArgs e)
		{
            int ok = 0;
			for (int i = 0; i < dgrDanhSach.SelectedRows.Count; i++)
			{

				try
				{
					int a = Convert.ToInt32(dgrDanhSach.SelectedRows[i].Cells["Mahd"].Value.ToString());
					var hd = dt.HoaDonBan_Delete(a);
                    log.Info(frmLogin.id+": đã xóa hóa đơn mã số ["+a+"]");
					dt.SubmitChanges();
				}
                catch (Exception) { log.Error(frmLogin.id + ": xóa hóa đơn thất bại"); }
			}
			BintoGrv();
		}
		int LayMotMaHD()
		{
			int a = 0;
			if (RowSelect.Count == 0)
				return 0;
			foreach (int i in RowSelect)
			{
				try
				{
					a = Convert.ToInt32(dgrDanhSach.Rows[i].Cells["Mahd"].Value.ToString());
					dt.SubmitChanges();
				}
				catch (Exception) { }
			}
			return a;
		}
		private void dgrDanhSach_DataSourceChanged(object sender, EventArgs e)
		{
			RowSelect.Clear();
		}

		void BindtoDtb()
		{
			XuLyHoaDon.ThemMoiHoaDon(txtNDHD.Text, Convert.ToDateTime(dttNgay.Value), frmLogin.id);
		}
		private void btnChiTiet_Click(object sender, EventArgs e)
		{
			frmMain frm = MdiParent as frmMain;
			int maso = XuLyDataGridView.GetInt(dgrDanhSach.CurrentRow, "Mahd");
			frm.btnChiTietHoaDon_Click(maso, e);
			log.Info(frmLogin.id + ": đã chọn chức năng xem chi tiết hóa đơn mã số [" + maso + "]");
		}
		private void btnTimKiem_Click(object sender, EventArgs e)
		{
			if (cbChoose.SelectedIndex == 0)
				dgrDanhSach.DataSource = XuLyHoaDon.HoaDon_laytheoMa(ConvertType.ToInt(txtSearch.Text));
			else
			{
				if (cbChoose.SelectedIndex == 1)
					dgrDanhSach.DataSource = XuLyHoaDon.layHoaDon_Ngay(dtTim.Value);
				else
				{
					if (cbChoose.SelectedIndex == 2)
						dgrDanhSach.DataSource = XuLyHoaDon.LayHoaDon_Thang(dtTim.Value);
					else
					{
						dgrDanhSach.DataSource = XuLyHoaDon.LayHoaDon_Nam(dtTim.Value);
					}
				}
			}
		}
		private void txtSearch_TextChanged_1(object sender, EventArgs e)
		{
			//if (cbChoose.SelectedIndex == 0)
			//{
			//    dgrDanhSach.DataSource = dt.HoaDonBan_SearchById(ConvertType.ToInt(txtSearch.Text));
			//}


		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
			XuLyHoaDon.UpdateTTHoaDon(LayMotMaHD(), txtNDHD.Text, Convert.ToDateTime(dttNgay.Value));
			BintoGrv();
			log.Info(frmLogin.id + ": đã cập nhật lại hóa đơn mã số [" + LayMotMaHD() + "]");
		}

		private void btnThemMoi_Click(object sender, EventArgs e)
		{
			expandablePanel1.Expanded = true;
			expandablePanel2.Expanded = true;
			expandablePanel3.Expanded = false;
			log.Info(frmLogin.id + ": đã chọn chức năng thêm mới hóa đơn");
		}

		private void btnXoa_Click(object sender, EventArgs e)
		{

		}

<<<<<<< .mine
		private void btnCancel_Click(object sender, EventArgs e)
		{
			Reset();
		}
		void Reset()
		{
			txtNDHD.Text = "";
			dttNgay.Value = DateTime.Now;
		}

		private void btnThongKe_Click(object sender, EventArgs e)
		{
			frmThongKe fTK = new frmThongKe();
			fTK.StartPosition = FormStartPosition.CenterParent;
			fTK.ShowDialog();
			log.Info(frmLogin.id + ": đã chọn chức năng thống kê thu chi");
		}

		private void btnTim_Click(object sender, EventArgs e)
		{
			expandablePanel1.Expanded = true;
			expandablePanel2.Expanded = false;
			expandablePanel3.Expanded = true;
		}
		XuLyDatHang xldh = new XuLyDatHang();

		private void btnIn_Click(object sender, EventArgs e)
		{
			int mahd = 0;
			if (dgrDanhSach.SelectedRows.Count > 0)
			{
				foreach (DataGridViewRow r in dgrDanhSach.SelectedRows)
				{
					mahd = Convert.ToInt32(r.Cells["Mahd"].Value.ToString());
					inhoadon = new frmInHoaDonBanHang(mahd);
				}
				inhoadon.ShowDialog();
				log.Info(frmLogin.id + ": đã chọn chức năng in hóa đơn mã số [" + mahd + "]");
			}

		}

		private void buttonItem2_Click(object sender, EventArgs e)
		{
			btnIn.PerformClick();
		}

		private void btnXoaHD_Click(object sender, EventArgs e)
		{
			btnXoadt.PerformClick();
		}

		private void btnCTHD_Click(object sender, EventArgs e)
		{
			btnChiTiet.PerformClick();
		}

=======
       
>>>>>>> .r56
        private void btnTimKiemHD_Click(object sender, EventArgs e)
        {
            expandablePanel3.Expanded = true;
        }





       


<<<<<<< .mine
	}
=======

    }
>>>>>>> .r56
}

